package csci6450.chickenFriedFS.nuggetClient.threads.functions;

import csci6450.chickenFriedFS.nuggetCore.ThreadStatus;
import csci6450.chickenFriedFS.nuggetCore.Nugget;
import csci6450.chickenFriedFS.nuggetCore.NuggetConstants;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jacobschoen
 */
public class PutNuggetOnServer extends Thread {

    private Nugget nugget;
    private String ipAddress;
    private int portNumber;
    private ObjectOutputStream outputStream = null;
    private ObjectInputStream inputStream = null;
    private Socket socket = null;
    private ThreadStatus status;

    public PutNuggetOnServer(Nugget nugget,ThreadStatus status) {
        this.nugget = nugget;
        this.ipAddress = nugget.getNuggetLocation();
        this.portNumber = nugget.getNuggetServerPort();
        this.status = status;
    }

    @Override
    public void run() {
        boolean success = false;
        try {
            this.initializeConnection();
            this.outputStream.writeObject(NuggetConstants.PUT);
            this.outputStream.flush();
            //should be a port number that the server will be listening on to receive
            //the file
            Integer serverResponse = (Integer) this.inputStream.readObject();
            //clost the first socket
            this.closeConnection();
            //socket to send the nugget
            Socket uploadFile = new Socket(this.ipAddress, serverResponse);
            this.outputStream = new ObjectOutputStream(uploadFile.getOutputStream());
            //write the nugget to the ouput stream
            this.outputStream.writeObject(nugget);
            this.outputStream.flush();
            //check if the server received the nugget successfully

            this.inputStream = new ObjectInputStream(uploadFile.getInputStream());
            uploadFile.isClosed();
            String operationStatus = (String) this.inputStream.readObject();
            if (operationStatus.equals(NuggetConstants.SUCCESS)) {
                success = true;
            }

            uploadFile.close();
        } catch (ClassNotFoundException ex) {
            success = false;
            ex.printStackTrace();
        } catch (IOException ex) {
            success = false;
            ex.printStackTrace();
        } catch (Exception e) {
            success = false;
            e.printStackTrace();
        }
        this.nugget = null;
        this.status.setSuccessful(success);
        this.status.setFinished(true);
    }

    private void initializeConnection() throws UnknownHostException, IOException {
        // open a sz streams for objects
        this.socket = new Socket(this.ipAddress, this.portNumber);
        this.outputStream = new ObjectOutputStream(socket.getOutputStream());
        this.inputStream = new ObjectInputStream(socket.getInputStream());
    }

    private void closeConnection() throws IOException {
        this.socket.close();
    }
}
